/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.GetCurrentUserChainRequest;
import me.zhengjie.modules.system.domain.QueryDisMemberRequest;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.WxUserDto;
import me.zhengjie.modules.system.service.dto.WxUserQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zhaoxiancheng
* @date 2021-11-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "微信用户管理")
@RequestMapping("/api/wxUser")
public class WxUserController {

    private final WxUserService wxUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('wxUser:list')")
    public void download(HttpServletResponse response, WxUserQueryCriteria criteria) throws IOException {
        wxUserService.download(wxUserService.queryAllUser(criteria), response);
    }

    @GetMapping
    @Log("查询微信用户")
    @ApiOperation("查询微信用户")
    @PreAuthorize("@el.check('wxUser:list')")
    public ResponseEntity<Object> query(WxUserQueryCriteria criteria, Pageable pageable){
        Map<String,Object> resultMap = wxUserService.queryAll(criteria,pageable);
        for (WxUserDto obj : (List<WxUserDto>)resultMap.get("content")) {
            if(obj.getHeadimgurl() == null || "".equals(obj.getHeadimgurl()))
            {
                obj.setHeadimgurl("http://wx.drzh-atmr.cn/static/images/head.jpg");
            }
        }
        return new ResponseEntity<>(resultMap,HttpStatus.OK);
    }

    @GetMapping("/disMember")
    @Log("查询分销会员")
    @ApiOperation("查询分销会员")
    //@PreAuthorize("@el.check('wxUser:list')")
    @AnonymousAccess
    public ResponseEntity<Object> queryDisMember(QueryDisMemberRequest memberRequest, Pageable pageable){
        Map<String,Object> resultMap = wxUserService.queryDisMemberAll(memberRequest,pageable);
        return new ResponseEntity<>(resultMap,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增微信用户")
    @ApiOperation("新增微信用户")
    @PreAuthorize("@el.check('wxUser:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody WxUser resources){
        return new ResponseEntity<>(wxUserService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改微信用户")
    @ApiOperation("修改微信用户")
    @PreAuthorize("@el.check('wxUser:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody WxUser resources){
        wxUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除微信用户")
    @ApiOperation("删除微信用户")
    @PreAuthorize("@el.check('wxUser:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        wxUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("统计微信用户")
    @ApiOperation("统计微信用户")
    @GetMapping("/count")
    public ResponseEntity<Object> count() {
        return new ResponseEntity<>(wxUserService.count(),HttpStatus.OK);
    }

    @PostMapping("getChainByOpenId")
    @Log("查询用户链条")
    @ApiOperation("查询用户链条")
    public ResponseEntity<Object> getChainByOpenId(@RequestBody WxUser resources){
        List<WxUserDto.UserChain> userChain = wxUserService.getUserChain(resources.getOpenId());
        return new ResponseEntity<>(userChain,HttpStatus.OK);
    }

    @GetMapping("getCurrentUserChain")
    @Log("出现当前登录用户链条")
    @ApiOperation("出现当前登录用户链条")
    public ResponseEntity<Object> getCurrentUserChain(GetCurrentUserChainRequest resources,Pageable pageable){
        Map<String, Object> userChain = wxUserService.getCurrentUserChain(resources,pageable);
        return new ResponseEntity<>(userChain,HttpStatus.OK);
    }
}